/* 
 * File:   PluginCtrlPane.cpp
 * Author: hao
 * 
 * Created on 2011年5月14日, 下午2:37
 * 
 */

#include "PluginCtrlPane.h"
#include <unistd.h>
#include <dlfcn.h>

PluginCtrlPane::PluginCtrlPane(const string & Path) {

    patch = Path;

    PluginCtrlPaneHandle = NULL;
    getArgCountFunction = NULL;
    getArgInformationFunction = NULL;
    getIntArgRangeFunction = NULL;
    getDoubleArgRangeFunction = NULL;
    getIntArgDataFunction = NULL;
    getDoubleArgDataFunction = NULL;
    getStringArgDataLengthFunction = NULL;
    getStringArgDataFunction = NULL;
    setIntArgDataFunction = NULL;
    setDoubleArgDataFunction = NULL;
    setStringArgDataFunction = NULL;
    getTypeFunction = NULL;
}

bool PluginCtrlPane::Init() {
    /*
     * PluginCtrlPane function set
     * unsigned getArgCount()
     * const char * getArgInformation( unsinged index );
     * bool getIntArgRange( unsigned index, int * pMax, int * pMin );
     * bool getDoubleArgRange( unsigned index, double * pMax, double * pMin );
     * bool getIntArgData( unsigned index, int * pout );
     * bool getDoubleArgData( unsigned index, double * pout );
     * unsigned getStringArgDataLength( unsigned index );
     * bool getStringArgData( unsigned index, const char * pout );
     * bool setIntArgData( unsigned index, int data );
     * bool setDoubleArgData( unsigned index, double data );
     * bool setStringArgData( unsigned index, const char * data );
     */

    if (PluginCtrlPaneHandle)
        return false;

    if (access(patch.c_str(), F_OK) == -1) {

        return false;

    }

    PluginCtrlPaneHandle = dlopen(patch.c_str(), RTLD_LAZY);
    if (!PluginCtrlPaneHandle)
        return false;

    getArgCountFunction = (unsigned(*)()) dlsym(PluginCtrlPaneHandle, "getArgCount");
    getArgInformationFunction = (const char * (*)(unsigned))dlsym(PluginCtrlPaneHandle, "getArgInformation");
    getIntArgRangeFunction = (bool(*)(unsigned, int *, int *))dlsym(PluginCtrlPaneHandle, "getIntArgRange");
    getDoubleArgRangeFunction = (bool(*)(unsigned, double *, double *))dlsym(PluginCtrlPaneHandle, "getDoubleArgRange");
    getIntArgDataFunction = (bool(*)(unsigned, int *))dlsym(PluginCtrlPaneHandle, "getIntArgData");
    getDoubleArgDataFunction = (bool(*)(unsigned, double *))dlsym(PluginCtrlPaneHandle, "getDoubleArgData");
    getStringArgDataLengthFunction = (unsigned(*)(unsigned)) dlsym(PluginCtrlPaneHandle, "getStringArgDataLength");
    getStringArgDataFunction = (bool(*)(unsigned, char *))dlsym(PluginCtrlPaneHandle, "getStringArgData");
    setIntArgDataFunction = (bool(*)(unsigned, int))dlsym(PluginCtrlPaneHandle, "setIntArgData");
    setDoubleArgDataFunction = (bool(*)(unsigned, double))dlsym(PluginCtrlPaneHandle, "setDoubleArgData");
    setStringArgDataFunction = (bool(*)(unsigned, const char *))dlsym(PluginCtrlPaneHandle, "setStringArgData");
    getTypeFunction = ( int(*)() )dlsym( PluginCtrlPaneHandle, "getType" );

    if (!getArgCountFunction || !getArgInformationFunction || !getIntArgRangeFunction 
            || !getDoubleArgRangeFunction || !getIntArgDataFunction || !getDoubleArgDataFunction 
            || !getStringArgDataLengthFunction || !getStringArgDataFunction 
            || !setIntArgDataFunction || !setDoubleArgDataFunction || !setStringArgDataFunction || !getTypeFunction )
        return false;


    return true;
}

unsigned PluginCtrlPane::getArgCount(){
    return getArgCountFunction();
}

const char * PluginCtrlPane::getArgInformation(unsigned ArgIndex){
    return getArgInformationFunction( ArgIndex );
}

bool PluginCtrlPane::getIntArgRange( unsigned ArgIndex, int * pMax, int * pMin ){
    return getIntArgRangeFunction( ArgIndex, pMax, pMin );
}

bool PluginCtrlPane::getDoubleArgRange(unsigned ArgIndex, double* pMax, double* pMin){
    return getDoubleArgRangeFunction( ArgIndex, pMax, pMin );
}

bool PluginCtrlPane::getIntArgData(unsigned ArgIndex, int* pout){
    return getIntArgDataFunction( ArgIndex, pout );
}

bool PluginCtrlPane::getDoubleArgData(unsigned ArgIndex, double* pout){
    return getDoubleArgDataFunction( ArgIndex, pout );
}

unsigned PluginCtrlPane::getStringArgDataLength(unsigned ArgIndex){
    return getStringArgDataLengthFunction( ArgIndex );    
}

bool PluginCtrlPane::getStringArgData(unsigned ArgIndex, char* pout){
    return getStringArgDataFunction( ArgIndex, pout );
}

bool PluginCtrlPane::setIntArgData(unsigned index, int data){
    return setIntArgDataFunction( index, data );
}

bool PluginCtrlPane::setDoubleArgData(unsigned index, double data){
    return setIntArgDataFunction( index, data );
}

bool PluginCtrlPane::setStringArgData(unsigned index, const char* data){
    return setStringArgDataFunction( index, data );
}

int PluginCtrlPane::getType(){
    return getTypeFunction();
}


PluginCtrlPane::~PluginCtrlPane() {
}

